package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.common.bean.PageParamVo;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import com.atguigu.gmall.sms.service.SkuLadderService;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.atguigu.gmall.sms.vo.SkuSaleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Autowired
    private SkuLadderService skuLadderService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }

//    @Transactional
    @Override
    public void saveSales(SkuSaleVo saleVo) {
        // 3.1保存sms_sku_bounds
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(saleVo,skuBoundsEntity);
        List<Integer> works = saleVo.getWork();
        if (!CollectionUtils.isEmpty(works)) {
            skuBoundsEntity.setWork(works.get(3) * 8 + works.get(2) * 4 + works.get(1) * 2 + works.get(0));
        }
        this.save(skuBoundsEntity);
        // 3.2保存sms_sku_full_reduction
        skuFullReductionService.saveSkuFullReduction(saleVo);
        // 3.3保存sms_sku_ladder
        skuLadderService.saveSkuLadder(saleVo);

//        int i = 1/0;
    }

    @Override
    public List<ItemSaleVo> querySalesBySkuId(Long skuId) {
        ArrayList<ItemSaleVo> itemSaleVos = new ArrayList<>();

        // 查询积分优惠
        SkuBoundsEntity skuBoundsEntity = this.getOne(new LambdaQueryWrapper<SkuBoundsEntity>().eq(SkuBoundsEntity::getSkuId, skuId));
        if (skuBoundsEntity != null) {
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("积分");
            itemSaleVo.setDesc("送" + skuBoundsEntity.getGrowBounds().setScale(0,BigDecimal.ROUND_DOWN) + "成长积分，送" + skuBoundsEntity.getBuyBounds().setScale(0,BigDecimal.ROUND_DOWN) + "购物积分");
            itemSaleVos.add(itemSaleVo);
        }

        // 查询满减
        SkuFullReductionEntity fullReductionEntity = this.skuFullReductionService.getOne(new LambdaQueryWrapper<SkuFullReductionEntity>().eq(SkuFullReductionEntity::getSkuId, skuId));
        if (fullReductionEntity != null) {
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc("满" + fullReductionEntity.getFullPrice().setScale(0,BigDecimal.ROUND_DOWN) + "减" + fullReductionEntity.getReducePrice().setScale(0,BigDecimal.ROUND_DOWN));
            itemSaleVos.add(itemSaleVo);
        }

        // 查询打折
        SkuLadderEntity ladderEntity = this.skuLadderService.getOne(new LambdaQueryWrapper<SkuLadderEntity>().eq(SkuLadderEntity::getSkuId, skuId));
        if (ladderEntity != null) {
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("打折");
            itemSaleVo.setDesc("满" + ladderEntity.getFullCount() + "件打" + ladderEntity.getDiscount().divide(new BigDecimal(10)).setScale(0,BigDecimal.ROUND_DOWN) + "折");
            itemSaleVos.add(itemSaleVo);
        }

        return itemSaleVos;
    }

}